package com.migu.rbac.util;

import com.alibaba.fastjson.JSON;
import com.migu.rbac.vo.Response;

import java.util.ArrayList;
import java.util.List;

/*
 *
 * @Description TODO :
 * @Author: zj
 * @Date: 9:34 2019/7/10
 * @Version 1.0
 *
 */

public class CheckPassword {
    public static final String PW_PATTERN = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$";

    public static final int FAIL = 5004;

    public static void main(String[] args) {
        String newPasswd = "Eye12345678-";
        String userName = "wer";
        Response a = check(newPasswd, userName);
        System.out.println(JSON.toJSONString(a));
    }

    public static Response check(String newPasswd, String userName) {
        if (!newPasswd.matches(PW_PATTERN)) {
            return Response.error(FAIL, "密码必须包含8位以上的大小写字母、数字与特殊符号");
        }

        if (verifyPasswordContainAccount(newPasswd, userName)) {
            return Response.error(FAIL, "密码不得包含用户名");
        }

        if (validateKey(newPasswd)) {
            return Response.error(FAIL, "新密码不能是键盘连续的字符");
        }
        return Response.success();
    }

    public static boolean validateKey(String str) {
        char[][] keyCode = {
                {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'},
                {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'},
                {'z', 'x', 'c', 'v', 'b', 'n', 'm'}
        };
        char[] c = str.toCharArray();
        List<Integer> x = new ArrayList<Integer>();
        List<Integer> y = new ArrayList<Integer>();
        for (int i = 0; i < c.length; i++) {
            char temp = c[i];
            toHere:
            for (int j = 0; j < keyCode.length; j++) {
                for (int k = 0; k < keyCode[j].length; k++) {
                    if (temp == keyCode[j][k]) {
                        x.add(j);
                        y.add(k);
                        break toHere;
                    }
                }
            }
        }
        boolean flag = false;
        for (int i = 0; i < x.size() - 2; i++) {
            // x                 b
            if (x.get(i) == x.get(i + 1) && x.get(i + 1) == x.get(i + 2)) { //三者在同一y行上
                if (y.get(i) > y.get(i + 2)) {
                    if (y.get(i) - 1 == y.get(i + 1) && y.get(i) - 2 == y.get(i + 2)) {
                        flag = true;
                        break;
                    }
                } else {
                    if (y.get(i) + 1 == y.get(i + 1) && y.get(i) + 2 == y.get(i + 2)) {
                        flag = true;
                        break;
                    }
                }

            } else if (x.get(i) != x.get(i + 1) && x.get(i + 1) != x.get(i + 2) && x.get(i) != x.get(i + 2)) { //三者均不在同一行上
                if (x.get(i) > x.get(i + 2)) {
                    if ((x.get(i) - 1 == x.get(i + 1) && x.get(i) - 2 == x.get(i + 2))) {
                        flag = true;
                    }
                } else {
                    if (x.get(i) + 1 == x.get(i + 1) && x.get(i) + 2 == x.get(i + 2)) {
                        flag = true;
                        break;
                    }
                }
            }

        }
        return flag;
    }

    public static boolean verifyPasswordContainAccount(String password, String account) {
        password = password.toLowerCase();
        account = account.toLowerCase();
        if (password.contains(account)) {
            return true;
        }
        String[] likes = {"a", "l", "o"};
        String[] likeSign = {"@", "!", "0"};
        String originalAccount = account + "";
        for (int i = 0; i < likes.length; i++) {
            String tempAccount = originalAccount.replace(likes[i], likeSign[i]);
            account = account.replace(likes[i], likeSign[i]);
            if (password.contains(tempAccount) || password.contains(account)) {
                return true;
            }
        }
        return false;
    }
}
